﻿using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;


namespace sydfdl.TMS.BLL
{
    /// <summary>
    /// 层级树业务逻辑层
    /// </summary>
    public class BLLT_B_HierarchyTree
    {
        /// <summary>
        /// 获取层级树所有节点
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllHierarchyTreeList()
        {
            DataTable dt = new DataTable();

            try
            {
                dt = SqlHelper.ExecuteProcedureToDataSet("P_B_HierarchyTree_GetAllList", null, "tree").Tables[0];
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }
            return dt;
        }

        /// <summary>
        /// 获取层级树所有可用根节点
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public DataTable GetAllHierarchyTreeRoot(string query)
        {
            DataTable dt = new DataTable();

            try
            {
                SqlParameter[] para = { new SqlParameter("@Query", SqlDbType.NVarChar, 50) };

                para[0].Value = query;

                dt = SqlHelper.ExecuteProcedureToDataSet("P_B_HierarchyTree_GetAllRoot", para, "tree").Tables[0];
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }
            return dt;
        }

        public DataTable GetAllHierarchyTreeLeaf(string type, string code, string value, string pageIndex, string pageSize, string sortField, string sortOrder, ref int cnt)
        {
            DataTable dt = new DataTable();

            try
            {
                SqlParameter[] para = { 
                                          new SqlParameter("@Type",SqlDbType.NVarChar,50),
                                          new SqlParameter("@Code",SqlDbType.NVarChar,50),
                                          new SqlParameter("@Value",SqlDbType.NVarChar,50),
                                          new SqlParameter("@PageIndex",SqlDbType.Int,4),
                                          new SqlParameter("@PageSize",SqlDbType.Int,4),
                                          new SqlParameter("@SortField",SqlDbType.NVarChar,50),
                                          new SqlParameter("@SortOrder",SqlDbType.NVarChar,50),
                                          new  SqlParameter("@Cnt",SqlDbType.Int,4)
                                      };
                para[0].Value = type;
                para[1].Value = code;
                para[2].Value = value;
                para[3].Value = string.IsNullOrEmpty(pageIndex) ? 0 : int.Parse(pageIndex);
                para[4].Value = string.IsNullOrEmpty(pageSize) ? 0 : int.Parse(pageSize);
                para[5].Value = string.IsNullOrEmpty(sortField) ? "DKey" : sortField;
                para[6].Value = string.IsNullOrEmpty(sortOrder) ? "ASC" : sortOrder;
                para[7].Direction = ParameterDirection.Output;

                dt = SqlHelper.ExecuteProcedureToDataSet("P_B_HierarchyTree_GetAllLeaf", para, "tree").Tables[0];

                cnt = para[7].Value == null ? 0 : int.Parse(para[7].Value.ToString());
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }

            return dt;
        }

        public string SaveTreeData(IEnumerable<T_B_HierarchyTree> add, IEnumerable<T_B_HierarchyTree> edit, IEnumerable<T_B_HierarchyTree> remove)
        {
            string addXML = EFHelp<T_B_HierarchyTree>.GetXML(add);
            string editXML = EFHelp<T_B_HierarchyTree>.GetXML(edit);
            string removeXML = EFHelp<T_B_HierarchyTree>.GetXML(remove);

            SqlParameter[] para = { 
                                      new SqlParameter("@AddXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@EditXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@RemoveXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@UserXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                  };
            para[0].Value = addXML;
            para[1].Value = editXML;
            para[2].Value = removeXML;
            para[3].Value = null;
            para[4].Direction = ParameterDirection.Output;

            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, "P_B_HierarchyTree_SaveTreeData", para);

            return (para[4].Value ?? string.Empty).ToString();
        }

    }
}
